﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ershou.DAL;

namespace Ershou.BLL
{
    public class yh
    {
        public int a = 0;
        DataClasses1DataContext db = new DataClasses1DataContext();

        public bool IsNameExist(string n)
        {
            yonghu customer = (from c in db.yonghu
                               where c.name == n
                               select c).FirstOrDefault();
            if (customer != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void Insert(string n, string p, string e)
        {
            yonghu customer = new yonghu
            {
                name = n,
                pwd = p,
                Email = e,
                image = "暂无图片.gif",
                keys = 1
            };
            db.yonghu.InsertOnSubmit(customer);
            db.SubmitChanges();
        }
        public int CheckLogin(string n, string p)
        {
            yonghu customer = (from c in db.yonghu
                               where c.name == n && c.pwd == p && (c.Sex != "已删除" || c.Sex == null)
                               select c).FirstOrDefault();
            if (customer != null)
            {
                return customer.id;
            }
            else
            {
                return 0;
            }
        }
        public int CheckLogin2(string n, string p)
        {
            yonghu customer = (from c in db.yonghu
                               where c.name == n && c.pwd == p && (c.Sex != "已删除" || c.Sex == null)
                               select c).FirstOrDefault();
            if (customer != null)
            {
                return customer.keys;
            }
            else
            {
                return 0;
            }
        }
        public void ChangePassword(int i, string p)
        {
            yonghu customer = (from c in db.yonghu
                                 where c.id == i
                                 select c).First();
            customer.pwd = p;
            db.SubmitChanges();
        }
        public void ResetPassword(string n, string e)
        {
            yonghu customer = (from c in db.yonghu
                                 where c.name == n && c.Email == e
                                 select c).First();
            customer.pwd = n;
            db.SubmitChanges();
        }

        

        public bool IsEmailExist(string n, string e)
        {
            yonghu customer = (from c in db.yonghu
                                 where c.name == n && c.Email == e
                                 select c).FirstOrDefault();
            if (customer != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public string select1(int n)
        {
            yonghu customer = (from c in db.yonghu
                               where c.id == n
                               select c).FirstOrDefault();
            if (customer != null)
            {
                return customer.name;
            }
            else
            {
                return "";
            }
        }
        public string select2(int n)
        {
            yonghu customer = (from c in db.yonghu
                               where c.id == n
                               select c).FirstOrDefault();
            if (customer != null)
            {
                return customer.Sex;
            }
            else
            {
                return "";
            }
        }
        public string select3(int n)
        {
            yonghu customer = (from c in db.yonghu
                               where c.id == n
                               select c).FirstOrDefault();
            if (customer != null)
            {
                return customer.Birth_Date.ToString();
            }
            else
            {
                return "";
            }
        }
        public string select4(int n)
        {
            yonghu customer = (from c in db.yonghu
                               where c.id == n
                               select c).FirstOrDefault();
            if (customer != null)
            {
                return customer.Telephone;
            }
            else
            {
                return "";
            }
        }
        public string select5(int n)
        {
            yonghu customer = (from c in db.yonghu
                               where c.id == n
                               select c).FirstOrDefault();
            if (customer != null)
            {
                return customer.Email;
            }
            else
            {
                return "";
            }
        }
        public string select6(int n)
        {
            yonghu customer = (from c in db.yonghu
                               where c.id == n
                               select c).FirstOrDefault();
            if (customer != null)
            {
                return customer.Adress;
            }
            else
            {
                return "";
            }
        }
        public string select7(int n)
        {
            yonghu customer = (from c in db.yonghu
                               where c.id == n
                               select c).FirstOrDefault();
            if (customer != null)
            {
                return customer.image;
            }
            else
            {
                return "";
            }
        }

        public string select8(int n)
        {
            yonghu customer = (from c in db.yonghu
                               where c.id == n
                               select c).FirstOrDefault();
            if (customer != null)
            {
                return customer.keys.ToString();
            }
            else
            {
                return "";
            }
        }

        public void Update1(int i,  string n,string s,string d,string t,string a,string img)
        {
            yonghu customer = (from c in db.yonghu
                               where c.id == i
                              select c).First();
            customer.name = n;
            customer.Sex = s;
            customer.Birth_Date = Convert.ToDateTime(d);
            customer.Telephone = t;
            customer.Adress = a;
            customer.image = img;
            db.SubmitChanges();
        }

        public void Update2(int i, string n, string s, string t, string a, string img)
        {
            yonghu customer = (from c in db.yonghu
                               where c.id == i
                               select c).First();
            customer.name = n;
            customer.Sex = s;
            customer.Telephone = t;
            customer.Adress = a;
            customer.image = img;
            db.SubmitChanges();
        }

        public List<shengfen> GetAllshengfen()
        {
            return (from c in db.shengfen
                    select c).ToList();
        }

        public int GetProductCountBysf(string name)
        {
            return (from p in db.jigou
                    where p.type == name
                    select p).Count();
        }

        public yonghu GetProductByProductId(int productId)
        {
            return (from p in db.yonghu
                    where p.id == productId
                    select p).First();
        }
        public List<yonghu> GetAllOrder()
        {
            return (from p in db.yonghu
                    where p.Sex!="已删除"
                    select p).ToList();
        }
        public List<shangpin> GetAllyhdd(int Id)
        {
            return (from c in db.paimaibiao join
                    u in db.shangpin on c.sp_id equals u.id
                    where c.yh_id==Id && u.size != "已删除"
                    orderby u.riqi descending
                    select u).ToList();
        }

        public void Update3(int i)
        {
            yonghu customer = (from c in db.yonghu
                               where c.id == i
                               select c).First();
            if (customer.keys == 1)
            {
                customer.keys = 2;
            }
            else if (customer.keys == 2)
            {
                customer.keys = 1;
            }
            else
            {
                throw new Exception("发生错误");
            }
            db.SubmitChanges();
        }
        public void Update4(int i)
        {
            yonghu customer = (from c in db.yonghu
                               where c.id == i
                               select c).First();
            if (customer.keys != 3)
            {
                customer.Sex = "已删除";
            }
            else
            {
                throw new Exception("发生错误");
            }
            db.SubmitChanges();
        }
    }
}